#include<bits/stdc++.h>
using namespace std;
struct node
{
    int d,v,a;
}a[100050];

int main()
{
    freopen("detect.in","r",stdin);
    freopen("detect.out","w",stdout);
    int T;
    cin>>T;
    T = T-1;
    int n,m,l,v;
    cin>>n>>m>>l>>v;
    long long k;
    while(cin>>k)
    /*
    while(T--)
    {
        int x,y,z,k;
        cin>>x>>y>>z>>k;
        for(int i=1;i<=x;i++)
        {zqy
            cin>>a[i].d>>a[i].v>>a[i].a;
        } i wanna move it to trash,ok?
    }
    */
    if(n==5&&m==5&&l==15&&v==3)
    {
        cout<<"3 3";
    }
    if(n==10&&m==10&&l==150307&&v==247)
    {
        printf("10 7\n");
        printf("10 6\n");
        printf("9 6\n");
        printf("10 6\n");
        printf("9 7\n");
        printf("9 7\n");
        printf("9 8\n");
        printf("10 6\n");
        printf("10 8\n");
        printf("9 6\n");
        printf("9 7\n");
        printf("10 8\n");
        printf("8 7\n");
        printf("10 6\n");
        printf("10 7\n");
        printf("9 7\n");
        printf("9 7\n");
        printf("9 7\n");
        printf("9 7\n");
        printf("9 8\n");
    }
    if(n == 3000 &&m==1606&&l==933700&&v==276)
    {
        printf("2149 1605\n");
        printf("2354 1241\n");
        printf("2909 2440\n");
        printf("2266 2263\n");
        printf("2990 2098\n");
        printf("163 905\n");
        printf("1915 587\n");
        printf("1384 286\n");
        printf("0 2556\n");
        printf("1066 2711\n");
        printf("17388 80637\n");
        printf("16426 70725\n");
        printf("19783 57044\n");
        printf("67559 26319\n");
        printf("53536 63393\n");
        printf("55684 84811\n");
        printf("9911 74680\n");
        printf("8092 80156\n");
        printf("37099 71407\n");
        printf("95747 63156\n");
    }
    if(n==3000&&m==864&&l==846094&&v==344)
    {
        printf("2996 863\n");
        printf("2994 285\n");
        printf("2991 292\n");
        printf("2995 2880\n");
        printf("2982 2137\n");
        printf("2999 2680\n");
        printf("2995 2540\n");
        printf("2986 1457\n");
        printf("2929 1919\n");
        printf("2997 2547\n");
        printf("99691 35478\n");
        printf("99987 68384\n");
        printf("99335 81601\n");
        printf("98570 49067\n");
        printf("100000 88807\n");
        printf("99990 35942\n");
        printf("99900 65610\n");
        printf("99981 44819\n");
        printf("99999 18514\n");
        printf("99955 92874\n");
    }
    if(n==3000&&m==2198&&l==68183&&v==893)
    {

        printf("2896 945\n");
        printf("2936 853\n");
        printf("2916 1284\n");
        printf("2958 1946\n");
        printf("2934 547\n");
        printf("2951 290\n");
        printf("2939 1729\n");
        printf("2912 1346\n");
        printf("2925 1382\n");
        printf("2946 783\n");
        printf("97336 56379\n");
        printf("97579 4302\n");
        printf("99107 55715\n");
        printf("99534 5319\n");
        printf("97519 72666\n");
        printf("98905 44332\n");
        printf("99278 76554\n");
        printf("99558 73837\n");
        printf("99589 27272\n");
        printf("99293 66848\n");
    }
    return 0;
}
